//
//  PicTableViewCell.m
//  MansApp
//
//  Created by lanou3g on 15/7/17.
//  Copyright (c) 2015年 ACH. All rights reserved.
//

#import "PicTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "UIImage+GIF.h"
@implementation PicTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        //标题
        self.titleLabel = [[UILabel alloc]initWithFrame:CGRectZero];
        self.titleLabel.frame = CGRectMake(kP6Height(20), kP6Height(10), kWidth - kP6Height(40), kP6Height(30));
        [self.contentView addSubview:_titleLabel];
        //内容(图片)
        self.contentImageView = [[UIImageView alloc]initWithFrame:CGRectZero];
        
        [self.contentView addSubview:_contentImageView];
        //赞
        self.goodButton = [UIButton buttonWithType:UIButtonTypeSystem];
        [self.goodButton setBackgroundImage:[UIImage imageNamed:@"Thumb-gray-16@2x.png"] forState:UIControlStateNormal];
        [self.contentView addSubview:_goodButton];
        //分享
        self.shareButton = [UIButton buttonWithType:UIButtonTypeSystem];
        [self.shareButton setBackgroundImage:[UIImage imageNamed:@"episode_icon_share@2x.png"] forState:UIControlStateNormal];
        [self.contentView addSubview:_shareButton];
        //中间分割线
        self.backView = [[UIView alloc]initWithFrame:CGRectZero];
        self.backView.backgroundColor = [UIColor grayColor];
        [self.contentView addSubview:_backView];
        //底部分割线
        self.lineView = [[UIView alloc]initWithFrame:CGRectZero];
        self.lineView.backgroundColor = [UIColor grayColor];
        [self.contentView addSubview:_lineView];
    }
    return self;
}
- (void)jsonWithUrl:(NSURL *)url
{
    self.contentImageView.frame = CGRectMake(kWidth / 2 - kP6Height(25),kHeight / 2 - kP6Height(30) - 64,kP6Height(50), kP6Height(60));
    
    [self.contentImageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"69E58PICPjk_1024.jpg"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        self.picHeight = image.size.height;
        self.picWidth = image.size.width;
        float scale = self.picWidth / (kWidth - kP6Height(40));
        float height = self.picHeight / scale;
        self.contentImageView.frame = CGRectMake(kP6Height(20), self.titleLabel.frame.size.height + kP6Height(20),kWidth - kP6Height(40), height);
    }];
    
}

- (void)cellSetModel:(PicModel *)model
{
    CGRect bounds = [model.title boundingRectWithSize:CGSizeMake(300, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:15] forKey:NSFontAttributeName] context:nil];
    self.titleLabel.frame = CGRectMake(kP6Height(20), kP6Height(10), kWidth - kP6Height(40), bounds.size.height);
    self.titleLabel.text = model.title;
    self.titleLabel.numberOfLines = 0;
    self.titleLabel.font = [UIFont fontWithName:@"MarkerFelt-Thin" size:15];
    NSURL *url = [NSURL URLWithString:model.thumbnail_pic];
    NSLog(@"url == %@ , string == %@",url,model.thumbnail_pic);

    [self.contentImageView sd_setImageWithURL:url];
    self.contentImageView.frame = CGRectMake(kP6Height(20), self.titleLabel.frame.size.height + kP6Height(20),kWidth - kP6Height(40), kP6Height(200));
    if ([model.detail_url hasSuffix:@".jpg"]) {
        // 截取最上方的视图
        self.contentImageView.contentMode = UIViewContentModeTop;
        self.contentImageView.clipsToBounds = YES;
    }
     
    self.goodButton.frame = CGRectMake(kP6Height(79), self.contentImageView.frame.origin.y + self.contentImageView.frame.size.height + kP6Height(5), kP6Height(20), kP6Height(20));
    
    self.shareButton.frame = CGRectMake((kWidth / 2) + kP6Height(79), self.contentImageView.frame.origin.y + self.contentImageView.frame.size.height + kP6Height(5), kP6Height(20), kP6Height(20));
    
    self.backView.frame = CGRectMake((kWidth / 2) - kP6Height(1), self.contentImageView.frame.origin.y + self.contentImageView.frame.size.height + kP6Height(5), kP6Height(0.5), kP6Height(25));
    
    self.lineView.frame = CGRectMake(kP6Height(20), self.backView.frame.origin.y + self.backView.frame.size.height + kP6Height(5), kWidth - kP6Height(40), kP6Height(0.5));
}
//趣图详情
- (void)cellSetDetailModel:(PicModel *)model
{
    CGRect bounds = [model.title boundingRectWithSize:CGSizeMake(300, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:15] forKey:NSFontAttributeName] context:nil];
    self.titleLabel.frame = CGRectMake(kP6Height(20), kP6Height(10), kWidth - kP6Height(40), bounds.size.height);
    self.titleLabel.text = model.title;
    self.titleLabel.numberOfLines = 0;
    self.titleLabel.font = [UIFont fontWithName:@"MarkerFelt-Thin" size:15];
    NSURL *url = [NSURL URLWithString:model.detail_url];
    [self jsonWithUrl:url];
}
//动态返回高度
- (CGFloat)height:(PicModel *)model
{
    CGRect bounds = [model.title boundingRectWithSize:CGSizeMake(300, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:14] forKey:NSFontAttributeName] context:nil];
    return bounds.size.height + kP6Height(200) + kP6Height(30) +kP6Height(35);
}
//详情界面动态返回高度
- (CGFloat)heightForDetail:(PicModel *)model
{
    __block CGFloat aaa = 0;
    NSURL *url = [NSURL URLWithString:model.detail_url];
    [self.contentImageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"5.jpg"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        self.picHeight = image.size.height;
        self.picWidth = image.size.width;
        float scale = self.picWidth / (kWidth - kP6Height(40));
        float height = self.picHeight / scale;
        aaa = height + kP6Height(30) + kP6Height(35);
    }];
    CGRect bounds = [model.title boundingRectWithSize:CGSizeMake(300, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:14] forKey:NSFontAttributeName] context:nil];
    return aaa + bounds.size.height;
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
